package forminrecipe.GUI;

import java.awt.Color;

import javax.swing.BoxLayout;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.table.TableColumn;

import forminrecipe.core.Core;
import forminrecipe.data.*;

public class RecipePanel extends JPanel {

	private BoxLayout recipeBoxLayout;
	private BoxLayout recipeIngreidentsBoxLayout;
	private BoxLayout recipeProcessBoxLayout;
	private JTextField recipeNameField;
	private JComboBox ratingComboBox;
	private JComboBox difficultyComboBox;
	private JComboBox catagoryComboBox;
	private JComboBox timeComboBox;
	private JTable ingredientsTable;
	private JTextArea processTextArea;	
	private JPanel recipeNamePanel;
	private JPanel recipeComboPanel;
	private JPanel recipeIngrientsPanel;
	private JPanel recipeProcessPanel;
	private JScrollPane ingredientsScrollPane;
	private Core coreRef;
	private IngredientsTableModel ingredientsTableModel;
	
	public RecipePanel(Core core) {
		coreRef = core;
		recipeNamePanel = new JPanel();
		recipeComboPanel = new JPanel();
		recipeIngrientsPanel = new JPanel();
		recipeProcessPanel = new JPanel();
		//recipeIngreidentsBoxLayout = new BoxLayout(recipeIngrientsPanel, BoxLayout.Y_AXIS);
		//recipeIngrientsPanel.setLayout(recipeIngreidentsBoxLayout);
		recipeBoxLayout = new BoxLayout(this, BoxLayout.Y_AXIS);
		this.setLayout(recipeBoxLayout);
		//recipeProcessBoxLayout = new BoxLayout(recipeProcessPanel, BoxLayout.Y_AXIS);
		//recipeProcessPanel.setLayout(recipeProcessBoxLayout);
		
		recipeNameField = new JTextField(constants.MAX_NAME_LENGTH);
		ratingComboBox = new JComboBox(constants.ratingValues);
		difficultyComboBox = new JComboBox(constants.difficultyValues);
		catagoryComboBox = new JComboBox();
		timeComboBox = new JComboBox(constants.timeValues);
		ingredientsTable = new JTable();
		ingredientsTableModel = new IngredientsTableModel();
		ingredientsTable.setModel(ingredientsTableModel);

		//TableColumn tempColumn = ingredientsTable.getColumnModel().getColumn(0);
		//tempColumn.setCellEditor(new DefaultCellEditor(new JComboBox(constants.unitValues)));


		ingredientsScrollPane=new JScrollPane();
		ingredientsScrollPane.add(ingredientsTable);
		
		processTextArea = new JTextArea("khjlhjh");

		recipeNamePanel.add(new JLabel("Name:"));
		recipeNamePanel.add(recipeNameField);
		recipeNamePanel.setBackground(new Color(2322343));
		
		recipeComboPanel.add(new JLabel("Rating:"));
		recipeComboPanel.add(ratingComboBox);
		recipeComboPanel.add(new JLabel("Difficulty:"));
		recipeComboPanel.add(difficultyComboBox);
		recipeComboPanel.add(new JLabel("Catagory:"));
		recipeComboPanel.add(catagoryComboBox);
		recipeComboPanel.add(new JLabel("Time (mins):"));
		recipeComboPanel.add(timeComboBox);
		recipeComboPanel.setBackground(new Color(23232));
		
		recipeIngrientsPanel.add(new JLabel("Ingredients:"));
		//recipeIngrientsPanel.add(ingredientsScrollPane);
		recipeIngrientsPanel.setBackground(new Color(94232));
		
		recipeProcessPanel.add(new JLabel("Process:"));
		recipeProcessPanel.add(processTextArea);
		recipeProcessPanel.setBackground(new Color(55555));
		this.add(recipeNamePanel);
		this.add(recipeComboPanel);
		this.add(recipeIngrientsPanel);
		this.add(recipeProcessPanel);
		
		setFeildsEditability(false);
	}
	public void saveRecipePassFromBrowser(){
		coreRef.saveRecipeDBM(recipeNameField.getText(), ratingComboBox.getSelectedIndex(), difficultyComboBox.getSelectedIndex(), 
				timeComboBox.getSelectedItem().toString(), catagoryComboBox.getSelectedItem().toString(), 
				processTextArea.getText());
		
		//todo - reload after saving
	}
	
	public void addCatagory(String catagory){
		catagoryComboBox.addItem(catagory);
	}
	
	public void resetAllFields(){
		recipeNameField.setText("");
		ratingComboBox.setSelectedIndex(0);
		difficultyComboBox.setSelectedIndex(0);
		timeComboBox.setSelectedIndex(0);
		catagoryComboBox.setSelectedIndex(0);
		processTextArea.setText("");
	}
	
	@SuppressWarnings("deprecation")
	public void setFeildsEditability(boolean bool){
		if(bool){
			ratingComboBox.enable();
			difficultyComboBox.enable();
			timeComboBox.enable();
			catagoryComboBox.enable();
		}else{
			ratingComboBox.disable();
			difficultyComboBox.disable();
			timeComboBox.disable();
			catagoryComboBox.disable();
		}
		recipeNameField.setEditable(bool);
		processTextArea.setEditable(bool);
	}
	
	public void addIngredients(String quanity, String unit, String item){
		ingredientsTableModel.addRow(quanity, unit, item);
		ingredientsTable.setModel(ingredientsTableModel);
	}
	
	public void displayRecipe(RecipeData recipeData) {
		recipeNameField.setText(recipeData.name);
		ratingComboBox.setSelectedIndex(recipeData.rating);
		difficultyComboBox.setSelectedIndex(recipeData.difficulty);
		timeComboBox.setSelectedItem(recipeData.time);
		processTextArea.setText(recipeData.process);
		catagoryComboBox.setSelectedItem(recipeData.catagory);
	}
}
